<?php

namespace App\Http\Controllers;

use App\PostTopic;
use Illuminate\Http\Request;
use \App\Topic;


class TopicController extends Controller
{
    //专题详情页
    public function show(Topic $topic)
    {
        //带文章数的专题
        $topic = Topic::withCount('postTopics')->find($topic->id);
        //专题文章列表
        $posts =$topic->posts()->orderBy('created_at', 'desc')->take(10)->get();
        $posts->load('user');
        //属于我的文章, && 未投稿
        $myposts = \App\Post::authorBy(\Auth::id())->topicNotBy($topic->id)->get();

        return view('topic/show', compact('topic', 'posts', 'myposts'));
    }

    //投稿
    public function submit(Topic $topic)
    {
        //验证
        $this->validate(\request(), [
           'post_ids' => 'required | array'
        ]);
        $post_ids = \request('post_ids');
        $topic_id = $topic->id;


        foreach($post_ids as $post_id)
        {
            $params = [
                'topic_id' => $topic_id,
                'post_id' => $post_id
            ];

            PostTopic::firstOrCreate($params);

        }
        return back();
    }
}
